package com.liu.qqRobot.wia.handler;

import cn.hutool.core.codec.Base64;
import com.liu.common.entity.vo.WIASyndicateMissionVO;
import com.liu.common.utils.HtmlToImageUtils;
import com.liu.common.utils.QQClientUtils;
import com.liu.common.utils.WIAClientUtils;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import com.liu.qqRobot.q.enums.OperationTypeEnum;
import freemarker.template.TemplateException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class SolarisHandler {

    @Resource
    private QGroupUserOperationQuantityService qGroupUserOperationQuantityService;

    /**
     * 处理事件
     * @param data 信息内容
     * @param groupId 群号
     * @param userId 发送人QQ号
     */
    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {
        WIASyndicateMissionVO wiaSyndicateMissionVO = new WIASyndicateMissionVO();
        try {
            wiaSyndicateMissionVO = WIAClientUtils.getSolaris();
        } catch (IOException | ClassNotFoundException | InvocationTargetException | IllegalAccessException |
                 NoSuchMethodException e) {
            log.error("获取warfrmae金星赏金信息失败:{}", e.getMessage(), e);
            return;
        }
        Map<String, Object> templateParam = new HashMap<>();
        templateParam.put("wiaSyndicateMissionVO", wiaSyndicateMissionVO);
        try {
            HtmlToImageUtils.htmlToImage("warframeSolarisTemplate.html", templateParam,
                    "src/main/resources/picture/warframeSolaris.png", 1800);
        } catch (IOException | ParserConfigurationException | TemplateException | SAXException e) {
            log.error("获取warfrmae金星赏金模板失败:{}", e.getMessage(), e);
            return;
        }
        File file = new File("src/main/resources/picture/warframeSolaris.png");
        String encode = Base64.encode(file);
        String msg = "[CQ:at,qq=" + userId + "]\n[CQ:image,file=base64://" + encode+ ",type=show,subType=0]";
        QQClientUtils.sendMsg(msg, userId, groupId, isGroup);
        qGroupUserOperationQuantityService.statisticsOperationQuantityByType(groupId, userId, OperationTypeEnum.WARFRAME_GET_SOLARI);
    }

}
